<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {   
        $nowdate = date('Y-m-d',time());
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        $visits = [];
 
        $group = 'FROM_UNIXTIME(createtime,"%Y-%m-%d")';
        $lastincome = Db::name('zhangdan')
            ->field('sum(price) as num,FROM_UNIXTIME(createtime,"%Y-%m-%d")as date')
            ->where('createtime','>=',$seventtime)
            ->where('status',1)
            ->group($group)
            ->select();
        $lastincome = array_column($lastincome, null,'date');
        
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            if(array_key_exists($day, $lastincome))
         
                $visits[$day] = $lastincome[$day]['num'];
            else
               $visits[$day] = 0;

        }
        $Orders = [];
        $group_order = 'FROM_UNIXTIME(createtime,"%Y-%m-%d")';
        $lastout = Db::name('zhangdan')
            ->field('sum(price) as num,FROM_UNIXTIME(createtime,"%Y-%m-%d")as date')
            ->where('createtime','>=',$seventtime)
            ->where('status',2)
            ->group($group_order)
            ->select();
        $lastout = array_column($lastout, null,'date');
        
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            if(array_key_exists($day, $lastout))
         
                $Orders[$day] = $lastout[$day]['num'];
            else
               $Orders[$day] = 0;

        }

        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        
        // $todayusersignupsql = "select count(*) as num from `fa_user` Where  date_format(from_unixtime(jointime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d')";
        // // 今日注册人数
        // $todayusersignup = Db::query($todayusersignupsql);
        // //今日登录人数
        // $todayuserloginsql = "select count(*) as num from `fa_user` Where  date_format(from_unixtime(logintime),'%Y-%m-%d') = date_format(now(),'%Y-%m-%d') ";
        // $todayuserlogin = Db::query($todayuserloginsql);
        //总用户人数
        // $totaluser = Db::execute("select count(*) as num from `fa_user`");
        

        $todayincome = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 1");

        $todayoutcome = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 2");

        $todayzaitu = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 3");
        $todayzaiku = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 4");
        $todayjie = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 5");
        $todayxian = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 6");
        $todaydian = Db::query("select IFNULL(sum(price),0.0) as income from `fa_zhangdan` where date_format(from_unixtime(createtime),'%Y-%m-%d') = ('$nowdate') and status = 7");

        $totalorder = Db::name('order')->count();
        $totalorderamount = Db::name('order')->where(['pay_status'=>20])->sum('total_price');

        $sum_out = Db::name('zhangdan')->where('status',2)->sum('price');
        $sum_in = Db::name('zhangdan')->where('status',1)->sum('price');
        $sum_zaitu = Db::name('zhangdan')->where('status',3)->sum('price');
        $sum_zaiku = Db::name('zhangdan')->where('status',4)->sum('price');
        $sum_jie = Db::name('zhangdan')->where('status',5)->sum('price');
        $sum_xian = Db::name('zhangdan')->where('status',6)->sum('price');
        $sum_dian = Db::name('zhangdan')->where('status',7)->sum('price');

        $all_zongzichan = $sum_in-$sum_out+$sum_zaitu+$sum_zaiku-$sum_jie+$sum_xian+$sum_dian;

        $this->view->assign([
            'all_zongzichan'=>$all_zongzichan,
            // 'totaluser'        => $totaluser,
            'todayincome'        => $todayincome[0]['income'],
            'todayoutcome'        => $todayoutcome[0]['income'],
            'todayzaitu'        => $todayzaitu[0]['income'],
            'todayzaiku'        => $todayzaiku[0]['income'],
            'todayjie'        => $todayjie[0]['income'],
            'todayxian'        => $todayxian[0]['income'],
            'todaydian'        => $todaydian[0]['income'],
            'sum_out'          => $sum_out,
            'sum_in'          => $sum_in,            
            'sum_zaitu'          => $sum_zaitu,            
            'sum_zaiku'          => $sum_zaiku,            
            'sum_jie'          => $sum_jie,            
            'sum_xian'          => $sum_xian,            
            'sum_dian'          => $sum_dian,            
            // 'todayuserlogin'   => $todayuserlogin['0']['num'],
            // 'todayusersignup'  => $todayusersignup['0']['num'],
            'todayorder'       => 2324,
            'unsettleorder'    => 132,
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            // 'paylist'          => $paylist,
            // 'createlist'       => $createlist,
            'totalorder'       => $totalorder,
            'totalorderamount' => $totalorderamount,
            'createdata'           => $Orders,
            'visits'           => $visits,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
